package nbcp.db.redis

import io.lettuce.core.api.sync.RedisCommands
import nbcp.base.comm.ApiResult
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import nbcp.base.comm.*
import nbcp.base.extend.*
import nbcp.base.utils.HttpUtil
import nbcp.base.utils.SpringUtil
import nbcp.db.redis.proxy.*

/**
 * Created by udi on 17-5-13.
 */
data class wx_access_token(
        var appId: String = "",
        var token: String = "",
        var expires_in: Int = 7200
)

@Suppress("SpringKotlinAutowiring")
@Configuration
@ComponentScan(basePackages = arrayOf("nbcp.db.redis"))
open class rer {
    companion object {
        open class SystemGroup {
            val ipcity = RedisStringProxy("ipcity", 3);
            //        val wxCorpUser = RedisJsonProxy(WxCorpUserInfo::class.java, "wxCorpUser", { it.id }, 2, MyUtil.OneDaySeconds * 7);
            val sysCityJson = RedisStringProxy("SysCityJson", 3);

            //金维度超级登录使用。
            val jwdToken = RedisStringProxy("JwdToken", 2, 60)

            val wxInfo = RedisStringProxy("WxInfo", 2)
            val wxOpenId = RedisStringProxy("wxOpenId", 2, 300)

            val systemVariable = RedisStringProxy("SysVariable", 2) //系统字典。

            //            var session = RedisSessionProxy("session",0,120)
            var sessionData = RedisSessionProxy("spring:session:sessions")
            //        val appSession = RedisJsonProxy(AppSessionData::class.java, "session", { it.id });
//        val pzxSession = RedisJsonProxy(PzxSessionData::class.java, "PzxSession", { it.id }, 0, 1200, true);
//        val jwdSession = RedisJsonProxy(JwdSessionData::class.java, "JwdSession", { it.id }, 0, 1200, true);

            val cacheSqlData = RedisStringProxy("")

            fun brokeKeys(table:String) = RedisSetProxy("broke-keys:${table}")
            var borkeKeysChangedVersion = RedisStringProxy("borke-keys-changed-version")
//            var borkeKeysSyncVersion = RedisStringProxy("borke-keys-sync-version")
            var brokingTable = RedisStringProxy("broking-table")
        }

        open class WxGroup {
            val access_token = RedisStringProxy("access_token",2, 300)

            fun getAccessToken(appId: String, appSecret: String): ApiResult<wx_access_token> {
                var token = rer.wx.access_token.get(appId).FromJson<wx_access_token>()
                var ret = ApiResult<wx_access_token>();
                ret.data = token
                if (token != null) {
                    return ret
                }

                var url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appId}&secret=${appSecret}"
                val ajax = HttpUtil(url);
                val res = ajax.doGet().FromJson<StringMap>();


                ret.msg = res["errmsg"].AsString();
                if (ret.msg.HasValue) {
                    return ret;
                }
                ret.data = wx_access_token(appId, res["access_token"].AsString(), res["expires_in"].AsInt())

                rer.wx.access_token.set(appId, ret.data!!.ToJson())
                return ret;
            }

        }

        open class CodeGroup {
            val jwdMaxSerialNumber = RedisStringProxy("JwdQRCodeMaxSerialNumber", 1);
            val minSerialNumber = RedisStringProxy("QRCodeMinSerialNumber", 1);
        }


        internal  val logger = LoggerFactory.getLogger(this::class.java.declaringClass)


        val sys by lazy { SystemGroup() }
        val wx by lazy { WxGroup() }
        val code by lazy { CodeGroup() }

//        val brokeTableChannelName = "brokeTables"
//
//        private val brokeTables_redis: RedisCommands<String, String> by lazy {
//            return@lazy SpringUtil.getBean<RedisConfig>().getRedis(0);
//        }
//
//        fun publish2brokeTables(msg:String){
//            brokeTables_redis.publish(brokeTableChannelName, msg)
//        }

    }
}
